﻿// *----------------------------------------------------------------
// Copyright (C) 2017 通通优品
// 版权所有。
// 
// 类名：IRedisCache.cs
// 功能描述：TongTongMall.Redis
// 
// Create User：jym 2017/02/09
// 
// Edit User：UserName,EditTime
// Describe：Describe
// ----------------------------------------------------------------*/
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TongTongMall.Runtime.Caching;

namespace TongTongMall.Redis.Cache
{
    /// <summary>
    /// redis扩展
    /// </summary>
    public interface IRedisCache : ICache
    {
        IDatabase Database { get; }

        /// <summary>
        /// 在一定时间段内获取集群服务器唯一标识，时间过后从1开始
        /// 原子类型的操作
        /// </summary>
        /// <param name="key">原子redisKey</param>
        /// <param name="timeSpan">（秒）原子过期时间，不指定1秒后过期</param>
        /// <returns></returns>
        long GetNextSequence(string key, TimeSpan? timeSpan = null);

        /// <summary>
        /// 删除模糊匹配的Key
        /// </summary>
        /// <param name="pattern"></param>
        void DeletePatternKey(string pattern);

        /// <summary>
        /// 删除模糊匹配的Key
        /// </summary>
        /// <param name="pattern"></param>
        Task DeletePatternKeyAsync(string pattern);

        /// <summary>
        /// 检查key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        bool Exists(string key);

        /// <summary>
        /// 检查key是否存在
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<bool> ExistsAsync(string key);

        /// <summary>
        /// 设置KEY的过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="expiresIn"></param>
        /// <returns></returns>
        void SetExpiration(string key, TimeSpan expiresIn);

        /// <summary>
        /// 设置KEY的过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <param name="expiresIn"></param>
        /// <returns></returns>
        Task SetExpirationAsync(string key, TimeSpan expiresIn);

        /// <summary>
        /// 获取key的过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        Task<TimeSpan?> GetExpirationAsync(string key);

        /// <summary>
        /// 获取key的过期时间
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        TimeSpan? GetExpiration(string key);

        /// <summary>
        /// 从缓存内获取内容
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key">key值</param>
        /// <param name="factory">缓存内获取不到内容时，从db或其他cache内读取的源</param>
        /// <param name="expire">过期时间</param>
        /// <returns></returns>
        Task<T> GetOrAddAsync<T>(string key, Func<string, Task<T>> factory, TimeSpan? expire);

        /// <summary>
        /// 获取匹配的key值
        /// </summary>
        /// <param name="pattern"></param>
        /// <returns></returns>
        List<string> GetPatterKeys(string pattern);

        #region Sets
        Task<bool> SetAddAsync(string key, params string[] values);
        Task<bool> SetContainsAsync(string key, string value);
        Task<bool> SetRemoveAsync(string key, params string[] values);
        #endregion

        #region Lists
        Task<bool> ListRightPushAsync(string key, params string[] values);
        Task<bool> ListLeftPushAsync(string key, params string[] values);
        Task<string> ListLeftPopAsync(string key);
        #endregion

        #region SortedSet
        Task<bool> SortedSetAddAsync(string key, IDictionary<string, double> values);
        Task<string[]> SortedSetRangeByScoreAsync(string key, double start = double.NegativeInfinity, double stop = double.PositiveInfinity, long skip = 0, long take = -1);
        Task<bool> SortedSetRemoveAsync(string key, params string[] values);
        //Task<bool> SortedSetRemoveRangeByValueAsync(string key, string value);
        #endregion
    }
}
